package com.nit.store.controller;

import com.nit.store.pojo.dto.input.UserDto;
import com.nit.store.pojo.entity.UserEntity;
import com.nit.store.pojo.vo.UserInfo;
import com.nit.store.pojo.vo.output.LoginOutput;
import com.nit.store.service.UserService;
import com.nit.store.util.JwtUtils;
import com.nit.store.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;




    @PostMapping("/reg")
    public JsonResult<Void> reg(UserDto userDto){
        System.out.println("参数是："+userDto.getUsername());
        userService.register(userDto);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    public JsonResult<LoginOutput> login(UserDto userDto){
        System.out.println("登录参数1是："+userDto.getUsername());
        System.out.println("登录参数2是："+userDto.getPassword());
        LoginOutput userLoginInfo = userService.login(userDto);
        return JsonResult.ok(userLoginInfo);
    }

    @PreAuthorize("hasAnyAuthority('/user/update')")
    @RequestMapping("/password/change")
    public JsonResult<Void> changePassword(String oldPassword,String newPassword,@RequestHeader(value="Authentication") String token){
        System.out.println("原密码："+oldPassword);
        String jwt = token.substring(tokenHeader.length());
        System.out.println("token："+jwt);
        Long uid = jwtUtils.parseJwtGetId(jwt);
        System.out.println("uid="+uid);
        userService.updatePassword(oldPassword,newPassword,uid);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAnyAuthority('/user/show')")
    @RequestMapping("/info/show")
    public JsonResult<UserEntity> showUserInfo(@RequestHeader(value="Authentication")String token){
        String jwt = token.substring(tokenHeader.length());
        Long uid = jwtUtils.parseJwtGetId(jwt);
        System.out.println("token："+jwt);
        System.out.println("uid="+uid);
        UserEntity user = userService.showUserInfo(uid);
        return JsonResult.ok(user);
    }

    @PreAuthorize("hasAnyAuthority('/user/update')")
    @RequestMapping("/info/change")
    public JsonResult<Void> updateUserInfo(UserInfo userInfo, @RequestHeader(value="Authentication")String token){
        String jwt = token.substring(tokenHeader.length());
        Long uid = jwtUtils.parseJwtGetId(jwt);
        System.out.println("token："+jwt);
        System.out.println("uid="+uid);
        userInfo.setUid(uid);
        log.info("{用户信息：}",userInfo);
        userService.updateUserInfo(userInfo);
        return JsonResult.ok();
    }



}
